package b1.scenes.prelude
{
	import b1.controls.basic.*;
	import b1.net.*;
	import b1.utils.*;
	
	import com.greensock.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	public class Prelude extends Sprite
	{
		public var preludeMc:BasicMc;
		private const edge:int = 20;
		private var textField:TextField;
		private var callBack:Function;
		
		public function Prelude(callBackFunc:Function)
		{
			super();
			callBack = callBackFunc;
			textField = new TextField();
			textField.selectable = false;
			textField.defaultTextFormat = new TextFormat("微软雅黑", 40);
			textField.text = "在一个月黑风高的晚上……";
			textField.x = 100;
			textField.y = 100;
			textField.width = 500;
			textField.textColor = 0xFFFFFF;
			textField.alpha = 0;
			var blackScene:Sprite = new Sprite();
			blackScene.graphics.beginFill(0x000000);
			blackScene.graphics.drawRect(0, 0, BFont.stageWidth, BFont.stageHeight);
			blackScene.alpha = 0;
			//两秒后显示黑屏
			TweenLite.to(blackScene, 2, {alpha:1, onComplete:showFirstText, onCompleteParams:[blackScene]});
			addChild(blackScene);
			addChild(textField);
		}
		
		//黑屏后5秒渐渐清晰文字
		private function showFirstText(blackScene:Sprite):void
		{
			blackScene.addChild(textField);	
			TweenLite.to(textField, 5, {alpha:1, onComplete:textDisappear, onCompleteParams:[blackScene]});
		}
		
		//清晰完毕后3秒消失
		private function textDisappear(blackScene:Sprite):void
		{
			TweenLite.to(textField, 3, {alpha:0, onComplete:beginToPlay, onCompleteParams:[blackScene]});
		}
		
		//文字消失后开始正常播放
		private function beginToPlay(blackScene:Sprite):void
		{
			removeChild(blackScene);
			//影片外框
			var bgrScene:Sprite = new Sprite();
			bgrScene.graphics.beginFill(0xC2B246);
			bgrScene.graphics.drawRect(0, 0, BFont.stageWidth, BFont.stageHeight);
			addChild(bgrScene);
			//影片
			var PreludeMc:Class = ResourceHelper.getInstance().getClassByName("Prelude");
			preludeMc = new BasicMc(PreludeMc, 20, false);
			preludeMc.x = -126.5;
			preludeMc.y = -165.4;
			addChild(preludeMc);
			//影片遮罩
			var maskSprite:Sprite = new Sprite();
			maskSprite.graphics.beginFill(0x000000);
			maskSprite.graphics.drawRect(edge, edge, BFont.stageWidth - 2 * edge, BFont.stageHeight - 2 * edge);
			preludeMc.mask = maskSprite;
			addChild(maskSprite);
			preludeMc.eventDispatcher.addEventListener(BasicMc.ON_MOVIE_FINISH, playPreludeFinal);
			//加载声音图标
			var soundIcon:SoundIcon = new SoundIcon();
			soundIcon.x = BFont.stageWidth - soundIcon.width - 30;
			soundIcon.y = 300;
			addChild(soundIcon);
		}
		
		//播放完成后渐黑，
		public function playPreludeFinal(event:Event):void
		{
			var blackScene:Sprite = new Sprite();
			blackScene.graphics.beginFill(0x000000);
			blackScene.graphics.drawRect(0, 0, BFont.stageWidth, BFont.stageHeight);
			blackScene.alpha = 0;
			textField = new TextField();
			textField.selectable = false;
			textField.defaultTextFormat = new TextFormat("微软雅黑", 40);
			textField.text = "几天后……";
			textField.textColor = 0xFFFFFF;
			textField.x = 100;
			textField.y = 100;
			textField.width = 500;
			textField.alpha = 0;		
			TweenLite.to(blackScene, 2, {alpha:1, onComplete:blackTextShow, onCompleteParams:[blackScene]});
			this.addChild(blackScene);
		}
		
		//显示文字
		private function blackTextShow(blackScene:Sprite):void
		{
			TweenLite.to(textField, 4, {alpha:1, onComplete:blackTextDisappear});
			blackScene.addChild(textField);
		}
		
		//文字消失
		private function blackTextDisappear():void
		{
			TweenLite.to(textField, 5, {alpha:0, onComplete:removeBlack});
		}
	
		//调用callback函数	
		private function removeBlack():void
		{
			this.callBack();
		}
		
	}
}